import axios from 'axios';
import type {
  BaseResponse,
  User,
  UserRegisterRequest,
  UserLoginRequest,
  UserUpdateMyRequest,
  Chart,
  ChartAddRequest,
  ChartEditRequest,
  ChartUpdateRequest,
  ChartQueryRequest,
  PageResponse,
  GenChartByAiRequest,
  BiResponse,
} from '@/types/api';

// 创建 axios 实例
const api = axios.create({
  // baseURL: 'http://localhost:8125/api',
  baseURL: 'https://bi-api.longwingstech.cn/api',
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    console.error('API Error:', error);
    return Promise.reject(error);
  }
);

// 用户相关 API
export const userApi = {
  // 用户注册
  register: (data: UserRegisterRequest): Promise<BaseResponse<number>> =>
    api.post('/user/register', data),

  // 用户登录
  login: (data: UserLoginRequest): Promise<BaseResponse<any>> =>
    api.post('/user/login', data),

  // 用户注销
  logout: (): Promise<BaseResponse<boolean>> =>
    api.post('/user/logout'),

  // 获取当前登录用户
  getLoginUser: (): Promise<BaseResponse<any>> =>
    api.get('/user/get/login'),

  // 更新个人信息
  updateMyUser: (data: UserUpdateMyRequest): Promise<BaseResponse<boolean>> =>
    api.post('/user/update/my', data),
};

// 图表相关 API
export const chartApi = {
  // 创建图表
  addChart: (data: ChartAddRequest): Promise<BaseResponse<number>> =>
    api.post('/chart/add', data),

  // 删除图表
  deleteChart: (id: number): Promise<BaseResponse<boolean>> =>
    api.post('/chart/delete', { id }),

  // 编辑图表
  editChart: (data: ChartEditRequest): Promise<BaseResponse<boolean>> =>
    api.post('/chart/edit', data),

  // 根据 id 获取图表
  getChartById: (id: number): Promise<BaseResponse<Chart>> =>
    api.get(`/chart/get?id=${id}`),

  // 分页获取图表列表
  listChartByPage: (data: ChartQueryRequest): Promise<BaseResponse<PageResponse<Chart>>> =>
    api.post('/chart/list/page', data),

  // 分页获取当前用户创建的图表列表
  listMyChartByPage: (data: ChartQueryRequest): Promise<BaseResponse<PageResponse<Chart>>> =>
    api.post('/chart/my/list/page', data),

  // 更新图表
  updateChart: (data: ChartUpdateRequest): Promise<BaseResponse<boolean>> =>
    api.post('/chart/update', data),

  // 智能分析（同步）
  genChartByAi: (data: FormData): Promise<BaseResponse<BiResponse>> =>
    api.post('/chart/gen', data, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }),

  // 智能分析（异步）
  genChartByAiAsync: (data: FormData): Promise<BaseResponse<BiResponse>> =>
    api.post('/chart/gen/async', data, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }),

  // 智能分析（异步消息队列）
  genChartByAiAsyncMq: (data: FormData): Promise<BaseResponse<BiResponse>> =>
    api.post('/chart/gen/async/mq', data, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }),
};

export default api;